<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- 顶部栏 -->
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>总条数：{{ total }}</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/uploadexcel')">导入excel</el-button>
          <el-button type="danger" size="small" @click="importExcel">导出excel</el-button>
          <el-button type="primary" size="small" @click="addEmployee">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-card style="margin-top: 10px;">
        <el-table :data="list" border>
          <el-table-column label="序号" type="index" width="80" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template #default="{row}">
              <el-image :src="row.staffPhoto" class="avatar" @click="createQr(scope.row)" />
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ formOf(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click.native="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column></el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination layout="prev, pager, next" :total="total" :page-size="params.size" @current-change="pageChange" />
        </div>
      </el-card>
    </div>
    <add-employee :show-dialog="showDialog" @close-dialog="closeDialog" @update-list="updateList" />
  </div>
</template>
<script>
import { GetEmployeeList, delEmployee } from '@/api/employees'
import AddEmployee from './components/add-employee.vue'
import { getExportData } from '@/utils/excelData'
export default {
  components: {
    AddEmployee
  },
  data() {
    return {
      list: [],
      params: {
        page: 1,
        size: 5
      },
      total: 0,
      showDialog: false
    }
  },
  mounted() {
    this.hgetEmployee()
  },
  methods: {
    // 获取员工列表
    async hgetEmployee() {
      const res = await GetEmployeeList(this.params)
      this.list = res.rows
      this.total = res.total
    },
    // 页码改变 刷新列表
    pageChange(page) {
      this.params.page = page
      this.hgetEmployee()
    },
    //  聘用形式 格式化
    formOf(type) {
      const map = {
        1: '正式',
        2: '非正式'
      }
      return map[type]
    },
    // 删除员工
    delEmployee(id) {
      this.$confirm('是否确认删除该员工?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除部门
        await delEmployee(id)
        // 刷新列表
        this.hgetEmployee()
        // 提示
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    // 添加员工
    addEmployee() {
      this.showDialog = true
    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false
    },
    // 更新列表
    updateList() {
      this.hgetEmployee()
    },
    // 导出excel
    async importExcel() {
      import('@/vendor/Export2Excel').then(async excel => {
        const res = await GetEmployeeList(this.params)
        // 1.准备表头中的中英文对应关系

        // 1.准备表头中的中英文对应关系
        const headerRelation = {
          '手机号': 'mobile',
          '姓名': 'username',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '转正日期': 'correctionTime',
          '部门': 'departmentName'
        }
        const { data, header } = getExportData(res.rows, headerRelation)
        excel.export_json_to_excel({
          header, // 表头数据
          data, // 表格数据 (二维数组)
          filename: '员工列表',
          autoWidth: true,
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    goDetail(id) {
      this.$router.push({
        path: '/employees/employeeDetail',
        query: {
          id
        }
      })
    }
  }
}

</script>

<style scoped>
.avatar{
  width: 100px;
  height: 100px;
  border-radius: 50%;
}
</style>
